﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Threading;
using Expenses.MvcUI.Infrastructure;
using System.Text;

namespace Expenses.MvcUI.Controllers
{
    [AddCultureToViewBagAttribute]
    public class HomeController : Controller
    {

        public ActionResult RedirectToPreferedCulture()
        {

            //WebHelper.BinaryDataHandler.GetRegistration(true, "");
            //WebHelper.ImageEdittingHandler.GetRegistration(true, "");
            //WebHelper.GradientImageHandler.GetRegistration(true, "");
            //WebHelper.SecurityModule.GetRegistration(true, "");

            string defaultCulture="en";
            if (this.RouteData.Values["culture"].ToString() == "")
            {
                if (this.Request.Cookies["culture"] != null && !string.IsNullOrEmpty(this.Request.Cookies["culture"].Value))
                {
                    defaultCulture = this.Request.Cookies["culture"].Value;
                }
                else if (this.Request.UserLanguages != null && this.Request.UserLanguages.Length > 0)
                {
                    defaultCulture = this.HttpContext.Request.UserLanguages[0];
                }
            }
            else
            {
                defaultCulture = this.RouteData.Values["culture"].ToString();
            }
            return RedirectToAction("Index", "Home", new { culture = defaultCulture });
        }
        public ActionResult ChangeCulture(string returnUrl,CultureInfo culture)
        {
            HttpCookie cookie = new HttpCookie("culture", culture.Name);
            cookie.Expires = DateTime.Now.AddYears(1);
            this.Response.Cookies.Add(cookie);
            if (returnUrl.Length > 1)
            {
                returnUrl = returnUrl.Substring(returnUrl.IndexOf("/", 1) + 1) + "/";
            }
            return Redirect("/" + culture.Name + "/" + returnUrl);
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }
    }
}
